package com.rybbaby.tss.front.tssMemberAttendClassRecord.controller;

import com.rybbaby.tss.core.annotation.RequestLog;
import com.rybbaby.tss.core.annotation.Token;
import com.rybbaby.tss.core.annotation.validator.RegexpValidator;
import com.rybbaby.tss.core.annotation.validator.StringValidator;
import com.rybbaby.tss.core.bean.PageBean;
import com.rybbaby.tss.core.hqlsearch.HqlGenerateUtil;
import com.rybbaby.tss.core.utils.AjaxJsonResponse;
import com.rybbaby.tss.core.utils.PropertiesReadUtil;
import com.rybbaby.tss.core.utils.SequenceUtil;
import com.rybbaby.tss.core.utils.StringUtils;
import com.rybbaby.tss.core.web.controller.BaseController;
import com.rybbaby.tss.front.tssClassAttendance.service.ITssClassAttendanceService;
import com.rybbaby.tss.front.tssClasses.service.ITssClassesService;
import com.rybbaby.tss.front.tssClassesLessons.service.ITssClassesLessonsService;
import com.rybbaby.tss.front.tssMemberAttendClassRecord.entity.TssMemberAttendClassRecord;
import com.rybbaby.tss.front.tssMemberAttendClassRecord.service.ITssMemberAttendClassRecordService;
import com.rybbaby.tss.front.tssMemberCheckAttendance.service.ITssMemberCheckAttendanceService;
import com.rybbaby.tss.front.tssMemberPackage.service.ITssMemberPackageService;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.PropertyProjection;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.Map;

/**
 * @author 雷春琼
 * @description 会员报班记录表管理
 * @createDate 2017-08-24 下午2:43:28
 */
@Controller
@RequestMapping(value = "tssMemberAttendClassRecordController")
public class TssMemberAttendClassRecordController extends BaseController {

    @Autowired
    private ITssMemberAttendClassRecordService tssMemberAttendClassRecordService;

    @Autowired
    private ITssClassesService tssClassesService;

    @Autowired
    private ITssMemberPackageService tssMemberPackageService;
    @Autowired
    private ITssClassesLessonsService tssClassesLessonsService;
    @Autowired
    private ITssMemberCheckAttendanceService tssMemberCheckAttendanceService;
    @Autowired
    private ITssClassAttendanceService tssClassAttendanceService;

    /**
     * 获取会员报班记录表列表
     *
     * @return
     * @throws IOException
     */
    @RequestLog(content = "获取会员报班记录表列表", save = true)
    @Token(validateToken = false)
    @RequestMapping(value = "tssMemberAttendClassRecordList", method = RequestMethod.POST)
    public ModelAndView tssMemberAttendClassRecordList(TssMemberAttendClassRecord entity, PageBean paramPageBean, @RequestParam(required = true) String targetdataId) {
        ModelAndView mav = new ModelAndView(PropertiesReadUtil.getStringProperty("tssMemberAttendClassRecordListView"));
        ModelAndView retrunMav = new ModelAndView(PropertiesReadUtil.getStringProperty("tssMemberReturnPcackageListView"));
        try {
            Map<String, String[]> unLockMap = getParameterMap(request);
            DetachedCriteria dc = DetachedCriteria.forClass(TssMemberAttendClassRecord.class);
            HqlGenerateUtil.fillDetachedCriteria(dc, entity, unLockMap);
            dc.add(Restrictions.eq("memberPackageId", targetdataId));
            PropertyProjection a = Projections.groupProperty("classId");
            PageBean pageBean = tssMemberAttendClassRecordService.pageList(dc, paramPageBean.getPageNo(), paramPageBean.getPageSize());
            mav.addObject(super.SEARCH_MAP, unLockMap);
            mav.addObject(super.PAGE_BEAN, pageBean);
            mav.addObject("searchFormId", SEARCH_FORM);
            mav.addObject("actionUrl", request.getRequestURI());
            mav.addObject("pagingBar", pageBean.getPagingBar(SEARCH_FORM));
            mav.addObject("serializeNum", SequenceUtil.getUpperCaseUUID());
            String requestURI = StringUtils.substringBeforeLast(request.getRequestURI(), "/");
            mav.addObject("editUrl", requestURI + "/toEditView.do");
            mav.addObject("deleteUrl", requestURI + "/delete.do");
            mav.addObject("batchDeleteUrl", requestURI + "/batchDelete.do");
        } catch (Exception e) {
            throw e;
        }
        return mav;
    }

  /*  *//**
     * 转到编辑视图
     *
     * @param entity       接收编辑参数的临时对象
     * @param serializeNum 请求标识
     * @return
     *//*
    @RequestLog(content = "转到会员报班记录表视图", save = true)
    @Token(validateToken = false)
    @RequestMapping(value = "toEditView", method = RequestMethod.GET)
    public ModelAndView toEditView(TssMemberAttendClassRecord entity, @RequestParam(required = true) String serializeNum, @RequestParam(required = true) String targetDataId, @RequestParam(required = true) String memberPackageId) {
        ModelAndView mav = new ModelAndView(PropertiesReadUtil.getStringProperty("tssMemberAttendClassRecordEditView"));
        if (entity != null && entity.getId() != null) {
            TssMemberAttendClassRecord editEntity = this.tssMemberAttendClassRecordService.get(entity.getId());
            mav.addObject("editEntity", editEntity);
            mav.addObject("editModel", "edit");
        } else {
            mav.addObject("editModel", "add");
        }


        mav.addObject("targetDataIds", targetDataId);
        List<Map<String, Object>> listMap = new ArrayList<>();
        //List<TssClasses> classesList=new ArrayList<>();
        String[] classesId = StringUtils.split(targetDataId, ",");
        //生成课表时间
        for (String ids : classesId) {
            Map<String, Object> map = new HashMap<>();
            TssClasses classes = this.tssClassesService.get(ids);
            //classesList.add(classes);
            map.put("classes", classes);
            String[] s = null;
            switch (classes.getWeekDay()) {
                case 1:
                    s = DateUtil.getDates(classes.getStartDate().toString(), classes.getEndDate().toString(), "星期一");
                    break;
                case 2:
                    s = DateUtil.getDates(classes.getStartDate().toString(), classes.getEndDate().toString(), "星期二");
                    break;
                case 3:
                    s = DateUtil.getDates(classes.getStartDate().toString(), classes.getEndDate().toString(), "星期三");
                    break;
                case 4:
                    s = DateUtil.getDates(classes.getStartDate().toString(), classes.getEndDate().toString(), "星期四");
                    break;
                case 5:
                    s = DateUtil.getDates(classes.getStartDate().toString(), classes.getEndDate().toString(), "星期五");
                    break;
                case 6:
                    s = DateUtil.getDates(classes.getStartDate().toString(), classes.getEndDate().toString(), "星期六");
                    break;
                case 7:
                    s = DateUtil.getDates(classes.getStartDate().toString(), classes.getEndDate().toString(), "星期日");
                    break;
            }
            List<Map<String, Object>> classDateList = this.tssMemberAttendClassRecordService.classDateList(memberPackageId, ids);
            map.put("classDate", s);
            map.put("classDateList", classDateList);
            listMap.add(map);

        }
        mav.addObject("memberPackageId", memberPackageId);
        mav.addObject("serializeNum", serializeNum);
        mav.addObject("listMap", listMap);
        mav.addObject("saveUrl", StringUtils.substringBeforeLast(request.getRequestURI(), "/") + "/saveOrUpdate.do");
        return mav;
    }*/

    /**
     * 保存编辑的对象
     *
     * @param entity 被编辑对象
     * @return
     */
    @ResponseBody
    @RequestLog(content = "保存会员报班记录表对象", save = true)
    @Token(validateToken = false)
    @RequestMapping(value = "saveOrUpdate", method = RequestMethod.POST)
    public String saveOrUpdate(TssMemberAttendClassRecord entity, @RequestParam(required = true) String targetDataId, @RequestParam(required = true) String memberPackageId) {
        AjaxJsonResponse ajr = new AjaxJsonResponse();
        try {
            String id = entity.getId();
            ajr.setResMsg("保存成功!");
            if (StringUtils.isNotBlank(id)) {
                TssMemberAttendClassRecord destEntity = this.tssMemberAttendClassRecordService.get(id);
                BeanUtils.copyProperties(entity, destEntity, "createTime");
                this.tssMemberAttendClassRecordService.updateEntitiy(destEntity);
            } else {
                //生成报班记录、空白班级考勤信息、考勤空白信息
                boolean bool = this.tssMemberAttendClassRecordService.saveEntity(entity, targetDataId, memberPackageId,true);
                //生成空白班级考勤信息
                //boolean classBool = tssClassAttendanceService.saveEntity(targetDataId);
                //生成考勤空白信息
                //boolean checkBool = this.tssMemberCheckAttendanceService.saveEntity(targetDataId, memberPackageId);

                if (bool) {
                    ajr.setResCode(200);
                    ajr.setResMsg("保存成功!");
                } else {
                    ajr.setResCode(301);
                    ajr.setResMsg("保存失败!");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            ajr.setResCode(500);
            ajr.setResMsg("保存失败(" + e.getMessage() + ")!");
        }
        return ajr.jsonResponse(false);
    }

    /**
     * 删除指定id的对象
     *
     * @param id 需要被删除的对象id
     * @return
     */
    @ResponseBody
    @RequestLog(content = "删除属性", save = true)
    @Token(validateToken = false)
    @RequestMapping(value = "delete", method = RequestMethod.GET)
    public String delete(@StringValidator(nullable = false) String id) {
        AjaxJsonResponse ajr = new AjaxJsonResponse();
        try {
            ajr.setResMsg("删除成功!");
            if (StringUtils.isNotBlank(id)) {
                this.tssMemberAttendClassRecordService.deleteEntityById(id);
            } else {
                ajr.setResMsg("删除失败!");
                ajr.setResCode(302);
            }
        } catch (Exception e) {
            e.printStackTrace();
            ajr.setResCode(500);
            ajr.setResMsg("删除失败(" + e.getMessage() + ")!");
        }
        return ajr.jsonResponse(false);
    }

    /**
     * 批量删除指定id的对象
     *
     * @param ids 需要被删除的对象id组成的字符串
     * @return
     */
    @ResponseBody
    @RequestLog(content = "删除属性", save = true)
    @Token(validateToken = false)
    @RequestMapping(value = "batchDelete", method = RequestMethod.GET)
    public String batchDelete(@RegexpValidator(nullable = false, regexp = "^(\\w+,?)+\\w+$") String ids) {
        AjaxJsonResponse ajr = new AjaxJsonResponse();
        try {
            ajr.setResMsg("删除成功!");
            if (StringUtils.isNotBlank(ids)) {
                boolean batchDelete = this.tssMemberAttendClassRecordService.batchDelete(ids.split(","));
                if (!batchDelete) {
                    ajr.setResMsg("删除失败!");
                    ajr.setResCode(302);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            ajr.setResCode(500);
            ajr.setResMsg("删除失败(" + e.getMessage() + ")!");
        }
        return ajr.jsonResponse(false);
    }


    /**
     * 获取会员可以退班的报班记录列表
     *
     * @return
     * @throws IOException
     */
    @RequestLog(content = "获取会员可以退班的报班记录列表", save = true)
    @Token(validateToken = false)
    @RequestMapping(value = "getShouldReturnClassPackageListByMemberId", method = RequestMethod.POST)
    public ModelAndView getShouldReturnClassPackageListByMemberId(TssMemberAttendClassRecord entity, PageBean paramPageBean, @RequestParam(required = true) String targetdataId, @RequestParam(required = true) Integer type) {
        ModelAndView mav = null;
        try {
            if (type == 1) {
                mav = new ModelAndView(PropertiesReadUtil.getStringProperty("tssMemberAttendJoinReturnClassRecordListView"));
            } else if (type == 2) {
                mav = new ModelAndView(PropertiesReadUtil.getStringProperty("tssTransitMemberAttendJoinReturnClassRecordListView"));
            }
            PageBean pageBean = tssMemberAttendClassRecordService.tssMemberAttendClassRecordListGroupByClass(targetdataId, paramPageBean.getPageNo(), paramPageBean.getPageSize());
            mav.addObject(super.PAGE_BEAN, pageBean);
            mav.addObject("searchFormId", SEARCH_FORM);
            mav.addObject("actionUrl", request.getRequestURI());
            mav.addObject("pagingBar", pageBean.getPagingBar(SEARCH_FORM));
            mav.addObject("serializeNum", SequenceUtil.getUpperCaseUUID());
            String requestURI = StringUtils.substringBeforeLast(request.getRequestURI(), "/");
            mav.addObject("editUrl", requestURI + "/toEditView.do");
            mav.addObject("deleteUrl", requestURI + "/delete.do");
            mav.addObject("batchDeleteUrl", requestURI + "/batchDelete.do");
        } catch (Exception e) {
            throw e;
        }
        return mav;
    }

    /**
     * 获取会员报班记录表列表
     *
     * @return
     * @throws IOException
     */
    @RequestLog(content = "获取会员报班记录表列表", save = true)
    @Token(validateToken = false)
    @RequestMapping(value = "tssMemberAttendClassRecordListGroupByClass", method = RequestMethod.POST)
    public ModelAndView tssMemberAttendClassRecordListGroupByClass(TssMemberAttendClassRecord entity, PageBean paramPageBean, @RequestParam(required = true) String targetdataId, @RequestParam(required = false) Integer type) {
        ModelAndView  mav = new ModelAndView(PropertiesReadUtil.getStringProperty("tssMemberAttendClassRecordListView"));
        try {
            PageBean pageBean = tssMemberAttendClassRecordService.tssMemberAttendClassRecordListGroupByClass(targetdataId, paramPageBean.getPageNo(), paramPageBean.getPageSize());
            mav.addObject(super.PAGE_BEAN, pageBean);
            mav.addObject("searchFormId", SEARCH_FORM);
            mav.addObject("actionUrl", request.getRequestURI());
            mav.addObject("pagingBar", pageBean.getPagingBar(SEARCH_FORM));
            mav.addObject("serializeNum", SequenceUtil.getUpperCaseUUID());
            String requestURI = StringUtils.substringBeforeLast(request.getRequestURI(), "/");
            mav.addObject("editUrl", requestURI + "/toEditView.do");
            mav.addObject("deleteUrl", requestURI + "/delete.do");
            mav.addObject("batchDeleteUrl", requestURI + "/batchDelete.do");
        } catch (Exception e) {
            throw e;
        }
        return mav;
    }
}
